/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cliente;

import interfaz_maaestro.InterfaceRemote;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author sala_a
 */
public class Cliente {
    
    private String coordinador;
    private Registry registry;
    private Map<String, Boolean> esclavos;
    private interfaz_maaestro.InterfaceRemote interfaz;

    
    public static String escucharCoordinador () {
        String ipCoordinador = new String();
        try {
            DatagramSocket socket = new DatagramSocket(6972, InetAddress.getByName("0.0.0.0"));
            socket.setBroadcast(true);
            byte[] mensaje = new byte[15000];
            DatagramPacket paquete = new DatagramPacket(mensaje, mensaje.length);
            socket.receive(paquete);
            System.out.println("ip: " + paquete.getAddress().getHostAddress());
            System.out.println("ip: " + new String(paquete.getData()));
            ipCoordinador = paquete.getAddress().getHostAddress();
        } catch (SocketException | UnknownHostException ex) {
            Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ipCoordinador;
    }
    
    public boolean conectarServidor() {
        try {
            coordinador = escucharCoordinador();
            registry = LocateRegistry.getRegistry(coordinador, 1099);
            interfaz = (interfaz_maaestro.InterfaceRemote)registry.lookup("maestro");
            esclavos = new HashMap<>();
            esclavos = interfaz.retornarMapa();
            return true;
        } catch (RemoteException | NotBoundException ex) {
            Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public Map<String, Boolean> getEsclavos() {
        return esclavos;
    }
    
    public String informarSO(String ip) throws Exception {
        return interfaz.informarSO(ip);
    }
    public String informarCpu(String ip) throws Exception {
        return interfaz.informarCpu(ip);
    }
    public String infoDiscoArchivos(String ip) throws Exception {
        return interfaz.infoDiscoArchivos(ip);
    }
    public String infoMemoria(String ip) throws Exception {
        return interfaz.infoMemoria(ip);
    }
    public String infoHilosProcesos(String ip) throws Exception {
        return interfaz.infoHilosProcesos(ip);
    }
    public String infoEstaticaCpu(String ip) throws Exception {
        return interfaz.infoEstaticaCpu(ip);
    }
}
